# -*- coding: utf-8 -*-
from datetime import timedelta
from utils.operators.spark_submit import SparkSubmitOperator
from jms.time_sensor import time_after_01_30

dt = '{{ execution_date | cst_ds }}'
nextdt = '{{ execution_date | date_add(1) | cst_ds }}'


loadSql = """
insert overwrite table jms_ods.tab_barscan_difficult_real_time partition(dt = 'assign_patition')
select
    recordid
    ,billcode
    ,listcode
    ,subbillcode
    ,transfercode
    ,packagecode
    ,scantype
    ,nextstation
    ,destination
    ,scanuser
    ,inputsite
    ,scantime
    ,inputtime
    ,operatedate
    ,send_deliver_user
    ,sendcustomer
    ,sendsite
    ,destsite
    ,transfercenter
    ,pcs
    ,weight
    ,goodstype
    ,expresstype
    ,shifts
    ,transfer_deliver_fee
    ,networkfee
    ,otherfee
    ,checkflag
    ,accountflag
    ,receiptflag
    ,returnflag
    ,elescaleflag
    ,unrecordflag
    ,writebackflag
    ,lockflag
    ,remark
    ,modifyuser
    ,modifytime
    ,mobile
    ,pistolid
    ,remark1
    ,remark2
    ,scansitecode
    ,uploadtime
    ,remark3
    ,scanusercode
    ,send_deliver_usercode
    ,nextstationcode
    ,source
    ,remark5
    ,remark6
    ,remark4
    ,scansiteid
    ,nextstationid
    ,scanuserid
    ,send_deliver_userid
    ,__delete_sign__
    ,__op_ts__
from
  json.(/KafkaDataRealtimeSync/tab_barscan_difficult/load_path/)
distribute by pmod(hash(rand()),80)
"""

# assign_patition = dt
# load_path = "{ today_path*,nextday_path--00 }".replace("nextday_path", "dt="+nextdt)\
#     .replace("today_path", "dt="+dt)\
#     .replace("`","@") #这里spark-submit会忽略`xx`的内容,所以这里先将`变成@，程序里面再转回来

assign_patition = dt
load_path = "{ today_path* }" \
    .replace("today_path", "dt="+dt) \
    .replace("`","@") #这里spark-submit会忽略`xx`的内容,所以这里先将`变成@，程序里面再转回来

jms_ods__tab_barscan_difficult_real_time = SparkSubmitOperator(
    task_id='jms_ods__tab_barscan_difficult_real_time',
    email=['yl_etl@yl-scm.com','yl_bigdata@yl-scm.com'],
    name='jms_ods__tab_barscan_difficult_real_time_{{ execution_date | date_add(1) | cst_ds }}',
    pool_slots=1,
    execution_timeout=timedelta(hours=2),
    driver_cores=4,
    driver_memory='16G',
    executor_cores=2,
    executor_memory='8G',
    num_executors=4,
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 20,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 60,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.sql.sources.partitionOverwriteMode': 'dynamic',  # 允许删改已存在的分区
          'spark.executor.memoryOverhead': '2G',  # 堆外内存
          'spark.sql.shuffle.partitions': 600,
          },
    java_class='com.yunlu.bigdata.jobs.etl.loadToHive.HdfsLoadToHiveDI',  # spark 主类
    application='hdfs:///scheduler/jms/spark/wangbiao/hdfsloadods/HdfsLoadToHiveDI.jar',  # spark jar 包
    yarn_queue='pro',
    application_args=[loadSql,assign_patition,load_path],
)
jms_ods__tab_barscan_difficult_real_time << time_after_01_30
